package com.yaoxun.bored.controller.sys;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.Constant;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.dict.DictItem;
import com.yaoxun.bored.model.sys.dict.DictItemQuery;
import com.yaoxun.bored.service.sys.dict.DictItemService;
import com.yaoxun.bored.util.enums.JsonStatus;
import com.yaoxun.bored.validator.ValidatorHelper;

import tk.mybatis.mapper.entity.Example;

@RequestMapping("/dictitem")
@Controller
public class DictItemController extends BaseController {
	
	private static String EDIT_PAGE = "sys/dict/item_edit";
	
	@Inject
	private DictItemService dictItemService;
	
	@Inject
	private ValidatorHelper validatorHelper;
	
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public ModelAndView save(@RequestParam Integer dictId) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		mv.addObject("dictId", dictId);
		return mv;
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(DictItem dictItem) {
		JsonObject jsonObject = new JsonObject();
		BindingResult bindingResult = validatorHelper.validate(dictItem);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return jsonObject;
		}
		dictItem.setCreateTime(new Date());
		dictItemService.insert(dictItem);
		return jsonObject;
	}
	
	/**
	 * 修改页面
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		DictItem dictItem = dictItemService.selectByPrimaryKey(id);
		mv.addObject(Constant.MV_OBJ_NAME, dictItem);
		return mv;
	}
	
	/**
	 * 修改实体
	 * @param dictItem
	 * @return
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject edit(DictItem dictItem) {
		JsonObject jsonObject = new JsonObject();
		BindingResult bindingResult = validatorHelper.validate(dictItem);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return jsonObject;
		}
		dictItemService.updateByPrimaryKeySelective(dictItem);
		return jsonObject;
	}
	
	/**
	 * 批量删除
	 * @param itemids
	 * @return
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject delete(@RequestParam("itemIds[]") Integer[] itemIds) {
		JsonObject jsonObject = new JsonObject();
		List<Integer> list = Arrays.asList(itemIds);
		Example example = new Example(DictItem.class);
		example.createCriteria().andIn("dictItemId", list);
		dictItemService.deleteByExample(example);
		return jsonObject;
	}
	
	@RequestMapping(value="/search")
	@ResponseBody
	public PageResult search(DictItemQuery query) {
		PageResult pageResult = new PageResult();
		query.setOrderBy("dict_item_id desc");
		PageInfo<Map<String,Object>> info = dictItemService.selectByPaging(query);
		pageResult.setTotal(info.getTotal());
		pageResult.setRows(info.getList());
		return pageResult;
	}
	
}
